package com.aso114.project.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.bean.JingPinBean;
import com.aso114.project.bean.QuestionBean;
import com.aso114.project.exam.QuestionTestActivity;
import com.aso114.project.mvp.LoginSP;
import com.aso114.project.mvp.activity.SelectedTopicPayActivity;
import com.aso114.project.mvp.model.CallBack;
import com.aso114.project.mvp.model.CommentModel;
import com.aso114.project.util.Constant;
import com.aso114.project.util.Helper;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectedTopicItemAdapter extends BaseAdapter {
    ArrayList<JingPinBean.PaperList> paperList = new ArrayList<>();
    private Context context;
    private int isLock = 0;

    public SelectedTopicItemAdapter(Context context, ArrayList<JingPinBean.PaperList> paperList, int isLock) {
        this.paperList = paperList;
        this.isLock = isLock;
        this.context = context;
    }

    @Override
    public int getCount() {
        return paperList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_selected_topic_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder.selectItemTitle.setText(paperList.get(position).getTitle());
        if (TextUtils.isEmpty(paperList.get(position).getUserCount())) {
            holder.selectItemB3.setText("已做过 0 次");
        } else {
            holder.selectItemB3.setText("已做过 " + paperList.get(position).getUserCount() + " 次");
        }
        if (TextUtils.isEmpty(paperList.get(position).getExamUserCount())) {
            holder.selectItemB2.setText("0 人已做:");
        } else {
            holder.selectItemB2.setText(paperList.get(position).getExamUserCount() + " 人已做");
        }

        holder.selectItemB1.setText("难度 " + paperList.get(position).getDifficultyFactor());
        holder.itemLy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                SelectedTopicPayActivity.statIntent(context);

                if (isLock == 1) {// 已购买
                    View dialogview = LayoutInflater.from(context).inflate(
                            R.layout.loading_view, null);
                    final Dialog dialog = new Dialog(context, R.style.dialogStyle);
                    dialog.setContentView(dialogview, new ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                    dialog.getWindow().setGravity(Gravity.CENTER);
                    // 设置点击外围解散
                    dialog.setCanceledOnTouchOutside(true);
                    dialog.setCancelable(true);//设置是否可以按退回键取消
                    dialog.show();
                    CommentModel.getInstant().get_question_bank(LoginSP.getInstance().getUserAreaId(), paperList.get(position).getPaperId(), Constant.adutst, new CallBack() {
                        @Override
                        public void onSuccess(Object o, boolean isSuccess, String msg) {
                            dialog.cancel();
                            Helper.gotoLogin(isSuccess);//token失效去重新登陆
                            QuestionBean qdata = (QuestionBean) o;
                            Constant.title_tv = "3";
                            EventBus.getDefault().postSticky(qdata);
                            Intent intent = new Intent(context, QuestionTestActivity.class);
                            intent.putExtra(QuestionTestActivity.IS_EXAM, true);
                            context.startActivity(intent);

                        }

                        @Override
                        public void onFailure(String fail) {
                            Helper.showToast("获取题库失败");
                            dialog.cancel();
                        }
                    });

                } else {// 未购买
                    SelectedTopicPayActivity.statIntent(context);
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.select_item_title)
        TextView selectItemTitle;
        @BindView(R.id.select_item_b1)
        TextView selectItemB1;
        @BindView(R.id.select_item_b2)
        TextView selectItemB2;
        @BindView(R.id.select_item_b3)
        TextView selectItemB3;
        @BindView(R.id.select_item_btn)
        ImageView selectItemBtn;
        @BindView(R.id.item_ly)
        ConstraintLayout itemLy;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
